Skip to main content

Java 8 Date Time API

Banner java icon

Java Date and Time: The Ultimate Upgrade πŸŽ‰β€‹

For years, Java developers struggled with Date and Calendar classes. They were cumbersome, confusing, and full of surprises (not the good kind). The Date class lacked essential features, and Calendar was as easy to use as a 90s VCR remote.

Then came Java 8, our knight in shining armor! πŸ¦Έβ€β™‚οΈ The new Date and Time API (JSR-310) revolutionized date handling with clarity, immutability, and ease of use.

πŸš€ Key Features​

  • Immutable & Thread-safe: No more accidental modifications.
  • Human-Readable Methods: plusDays(5), minusWeeks(2), isLeapYear(), etc.
  • Built-in Time Zones & Adjusters: Finally, no more timezone nightmares!

1️⃣ Working with Dates and Times​

πŸ“… 1.1 LocalDate: Date Without Time​

LocalDate today = LocalDate.now();
System.out.println(today); // 2025-02-27

⏰ 1.2 LocalTime: Time Without Date​

LocalTime now = LocalTime.now();
System.out.println(now); // 14:30:15.123

πŸ“† 1.3 LocalDateTime: Both Date and Time​

LocalDateTime timestamp = LocalDateTime.now();
System.out.println(timestamp); // 2025-02-27T14:30:15.123

🌍 1.4 ZonedDateTime: Timezone-Aware​

ZonedDateTime zonedNow = ZonedDateTime.now(ZoneId.of("Asia/Tokyo"));
System.out.println(zonedNow); // 2025-02-27T14:30+09:00[Asia/Tokyo]

2️⃣ Measuring Time Intervals​

⏳ 2.1 Instant: Timestamps in UTC​

Instant instant = Instant.now();
System.out.println(instant); // 2025-02-27T05:30:15.123Z

⏲️ 2.2 Duration: Time Between Instants​

Duration gap = Duration.between(instant, instant.plusSeconds(600));
System.out.println(gap.toMinutes()); // 10

πŸ“† 2.3 Period: Difference Between Dates​

Period period = Period.between(LocalDate.of(2020, 2, 27), LocalDate.of(2025, 2, 27));
System.out.println(period.getYears()); // 5

3️⃣ Utility Classes & Formatting​

πŸ—“οΈ Adjusting Dates​

LocalDate nextSunday = LocalDate.now().with(TemporalAdjusters.next(DayOfWeek.SUNDAY));
System.out.println(nextSunday); // 2025-03-02

🎨 Formatting Dates​

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd MMM yyyy, HH:mm");
System.out.println(LocalDateTime.now().format(formatter)); // 27 Feb 2025, 14:30

The Java 8 Date & Time API is a massive improvement, making it easier and safer to work with dates. No more juggling Date, Calendar, and SimpleDateFormat.

Happy Coding! πŸš€